package work.timejay.user.controller.wechat;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import work.timejay.user.utils.WeChatUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @className:QRCodeController.java
 * @author:王文杰
 * @version:1.0.0
 * @description:TODO
 * @createTime 2022年04月11日 08:57:00
 */
@RestController
@RequestMapping(value = "/api/wechat/qr")
@Tag(name = "微信二维码接口层",description = "QRCodeController")
public class QRCodeController {

    /**
     * 生成临时二维码
     *
     * @param data
     * @return
     */
    @GetMapping(value = "/{data}")
    public String createQRCode(@PathVariable("data") String data) throws IOException {
        //获取accessToken
        String jsonStr = WeChatUtils.getTestAccessToken();
        JSONObject json = JSONUtil.parseObj(jsonStr);
        String accessToken = json.getStr("access_token");
        //获取qrCode
        Map<String, Object> map = new HashMap<>();
        map.put("action_name", "QR_STR_SCENE");
        map.put("expire_seconds", "604800");
        Map<String, Object> order = new HashMap<>();
        Map<String, Object> item = new HashMap<>();
        item.put("scene_str", "test");
        order.put("scene", item);
        map.put("action_info", order);
        //获取QrCode
        return WeChatUtils.getQRCode(accessToken, map);
    }

    @GetMapping(value = "/file")
    public InputStream getFile(@RequestParam("code") String code) throws IOException {
        //获取QrCode
        return WeChatUtils.getFile(code);
    }

}
